//
//  QAuthViewController.swift
//  Weibo
//
//  Created by 孙成宇 on 15/11/27.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

import SVProgressHUD


class OAuthViewController: UIViewController {

    let webView = UIWebView()
    
    
    @objc private func close(){
    
  
    dismissViewControllerAnimated(true, completion: nil)
        
    
    
    }
    override func loadView() {
        view = webView
      // webView.delegate = self
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置导航条
        
         navigationItem.leftBarButtonItem = UIBarButtonItem(title:"关闭", style: .Plain ,target: self , action: "close")
        
        loadOauthPage()

        
    }
    
    //MARK 加载授权界面
    private func loadOauthPage(){
    
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        
        if let url = NSURL(string: urlString){
        
        let request = NSURLRequest(URL: url)
        
        webView.loadRequest(request)
        
    
        }
    
    
    }
    
    
    
    
    
//      extension OAuthViewController: UIWebViewDelegate  {
//        func webViewDIdFinisheLoad(webView : UIWebView) {
//        
//        SVProgressHUD.dismiss()
//        
//        
//        }
//        func webViewDidStartLoad(webView: UIWebView) {
//        
//        SVProgressHUD.show()
//        
//        }
//    
//    }
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        print(request)
        let urlString = request.URL?.absoluteString ?? ""
        

    //屏蔽掉不希望加载的页面
    if urlString.hasPrefix("https://api.weibo.com/") {
    return true
    }
    
    if !urlString.hasPrefix("http://www.itcast.cn") {
    return false
    }
    
    
    //程序如果走到这里  就只有包含回调的url才能运行到此
    
    
    return false

    }

}
